package com.gable.report.controller;

import com.gable.report.common.util.MD5Util;
import com.gable.report.dao.IReportAuthDao;
import com.gable.report.model.ReportAuth;
import com.gable.report.model.User;
import com.gable.report.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 * @author Aioria
 *
 * 2017-9-20 报表登录修改 只提供青中心有权限的账户
 */
@Controller
public class LoginController {
	
	private final Log logger = LogFactory.getLog(this.getClass());

	@Resource
	HttpServletRequest request;
	
	@Resource
	HttpServletResponse response;
	
	@Autowired
	UserService userService;

	@Autowired
	IReportAuthDao reportAuthDao;
	
	/**
	 * 进入登录页面
	 * @return
	 */
	@RequestMapping(value="/login-error",method=RequestMethod.GET)
	public String toLogin(){
		return "login";
	}
	
	/**
	 * 功能说明：用户登陆模块
	 * @throws IOException 
	 * @return,
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username,String password,RedirectAttributes attr) throws IOException{
		// 接受返回值Map
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap = userService.login(username, MD5Util.md5(password));	//MD5Util.md5()给密码加密
		// 取出登陆状态
		String result = resultMap.get("result").toString();
		// 1表示登陆成功，如果登陆成功就向session放参数
		if("1".equals(result)){
			User user = (User) resultMap.get("loginUser");

			//医院用户
			if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
				ReportAuth reportAuth = reportAuthDao.queryByHospitalId(user.getHospitalId());
				//是否有权限查看报表
				if(reportAuth == null || reportAuth.getReportTag() == 0){
					return "charts-authority";
				}
				//医院管理员和设备科有权限登录报表
				if(user.getRoleId() == null || user.getRoleId() != 6 && user.getRoleId() !=1){
					return "charts-authority";
				}
			}
			HttpSession session=request.getSession();								// 创建session对象
			session.setAttribute("loginUser", resultMap.get("loginUser")); 			// 把user对象放入session
			session.setAttribute("longName", user.getName());						// 当前登陆用户名
			session.setAttribute("longRole", user.getRole());						// 当前登陆角色
			session.setAttribute("phone", user.getPhone());							// 当前登陆手机号码
			return "redirect:/index/page";

		}else if("0".equals(result)){  
			attr.addFlashAttribute("username",username);
			attr.addFlashAttribute("msg", "账号不存在！");
			return "redirect:/login-error";
		}else if("2".equals(result)){ 
			attr.addFlashAttribute("username",username);
			attr.addFlashAttribute("msg", "密码错误！");
			return "redirect:/login-error";
		}else if("3".equals(result)){
			attr.addFlashAttribute("username",username);
			attr.addFlashAttribute("msg", "该账户已经被禁用");
			return "redirect:/login-error";
		}else{
			attr.addFlashAttribute("username",username);
			attr.addFlashAttribute("msg", "登陆异常");
			return "redirect:/login-error";
		}
	}
	
	/**
	 * 功能说明：退出登陆注销session
	 * 创建人：乔春峰
	 * 创建时间：2016年9月13日 19:51:27
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="logout",method=RequestMethod.GET)
	public String logout() throws IOException{
		HttpSession session=request.getSession();	
		session.invalidate();
		return "redirect:/";
	}
	
	@RequestMapping(value="/authorityOut")
	public String authorityOut(){
		return "authority500";
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  清空筛选
	 * @Date: 2018/6/4 16:31
	 * @return
     */
	@RequestMapping(value="/login-clear",method=RequestMethod.POST)
	@ResponseBody
	public String LoginFlush(){
		HttpSession session=request.getSession();
		session.invalidate();
		return "success";
	}
}
